package com.zmn.mcc.business.impl.dpermit.measure;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.cube.common.dro.measure.MeasureDRO;
import com.zmn.mcc.business.interfaces.dpermit.measure.DpermitCubeMeasureBService;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.manager.utils.DpermitUtil;
import com.zmn.mcc.model.dto.dpermit.DpermitSimpleDO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DpermitCubeMeasureBServiceImpl implements DpermitCubeMeasureBService {
    @Override
    public List<DpermitSimpleDO> listMeasureAndDpermitChecked(int status, List<Integer> assignedMeasureIds, List<MeasureDRO> measures) {
        // 指标排序
        //measures = measures.stream()
        //        .sorted(
        //                Comparator.comparing(MeasureDRO::getLevel)
        //                        //.reversed()
        //                        .thenComparing(MeasureDRO::getParentId)
        //                        //.reversed()
        //                        .thenComparing(MeasureDRO::getMeasureId)
        //                        //.reversed()
        //        )
        //        .collect(Collectors.toList());

        List<DpermitSimpleDO> items = new ArrayList<>();
        Map<Integer, DpermitSimpleDO> map = new HashMap<>();

        for (MeasureDRO measure : measures) {
            // 过滤异常数据
            if (measure.getLevel() == null) {
                continue;
            }

            // 转换为简单对象
            DpermitSimpleDO simple = this.parseToDpermitSimpleDO(measure);

            // 二级加入映射关系，方便后面查找上级
            if (measure.getLevel() == DpermitConsts.MEASURE_LEVEL_CATEGORY) {
                map.put(simple.getDataId(), simple);
            }

            // 选中已分配
            if (CollectionUtil.isNotNullOrEmpty(assignedMeasureIds) && assignedMeasureIds.contains(simple.getDataId())) {
                simple.setChecked(true);
            }

            // 一级、二级加入主列表
            if (measure.getLevel() <= DpermitConsts.MEASURE_LEVEL_CATEGORY) {
                items.add(simple);
            }
            // 三级加入二级的子集
            else if (measure.getLevel() == DpermitConsts.MEASURE_LEVEL_MEASURE) {
                DpermitSimpleDO parent = map.get(simple.getParentId());
                if (parent != null) {
                    parent.getSubors().add(simple);
                }
            }
        }

        return items;
    }

    @Override
    public List<DpermitSimpleDO> listMeasureTreeAndDpermitChecked(int status, List<Integer> dpermitIds, List<MeasureDRO> measures) {
        List<DpermitSimpleDO> items = new ArrayList<>();

        // 指标ID和简单指标对象的映射关系
        Map<Integer, DpermitSimpleDO> map = new HashMap<>();

        // 一级、二级、三级指标列表
        for (MeasureDRO measure : measures) {
            // 防止数据错误抛出异常
            if (measure.getLevel() == null) {
                continue;
            }

            // 简单菜单对象
            DpermitSimpleDO simple = this.parseToDpermitSimpleDO(measure);

            // 一级、二级加入映射关系map
            if (measure.getLevel() <= DpermitConsts.MEASURE_LEVEL_CATEGORY) {
                map.put(simple.getDataId(), simple);
            }

            // 选中已分配
            if (CollectionUtil.isNotNullOrEmpty(dpermitIds) && dpermitIds.contains(simple.getDataId())) {
                simple.setChecked(true);
            }

            // 一级加入主列表
            if (measure.getLevel() == DpermitConsts.MEASURE_LEVEL_DATASET) {
                items.add(simple);
            }
            // 加入上级指标的子集
            else if (measure.getLevel() >= DpermitConsts.MEASURE_LEVEL_CATEGORY) {
                DpermitSimpleDO parent = map.get(simple.getParentId());
                if (parent != null) {
                    parent.getSubors().add(simple);
                }
            }
        }

        /*
         * 检查并处理简单数据对象选中状态
         * 下级部分选中则上级选中，下级全部选中则对上级标识
         */
        DpermitUtil.checkSelected(items, false);
        return items;
    }

    /**
     * 转换为简单数据权限对象
     *
     * @param measure
     * @return
     */
    private DpermitSimpleDO parseToDpermitSimpleDO(MeasureDRO measure) {
        if (measure == null) {
            return null;
        }
        DpermitSimpleDO simple = new DpermitSimpleDO();
        simple.setDataId(measure.getMeasureId());
        simple.setDataName(measure.getMeasureName());
        simple.setParentId(measure.getParentId());
        simple.setStatus(measure.getStatus());
        simple.setLevel(measure.getLevel());
        return simple;
    }
}
